package com.dk.controller;

import com.dk.em.Result;
import com.dk.page.PageResult;
import com.dk.pojo.Reservoir;
import com.dk.service.ReservoirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("reservoir")
@Api(tags = "库区服务")
@RefreshScope       //开启热更新
public class ReservoirController {

    @Autowired
    private ReservoirService reservoirService;

    /*增加ok*/
    @PostMapping
    @ApiOperation("库区增加接口")
    @ApiImplicitParam(name = "库区对象信息",value = "reservoir",required = false)
    public Result insertreservoir(@RequestBody Reservoir reservoir){
        reservoirService.insertreservoir(reservoir);
        return Result.OK();
    }


    /*根据id删除ok*/
    @DeleteMapping("deleteById")
    @ApiOperation("根据id删除")
    public Result deletereservoir(Integer id){
        reservoirService.deleteById(id);
        return Result.OK();
    }


    //批量删除ok
    @DeleteMapping
    @ApiOperation("根据id批量删除")
    public Result deleteAllreservoir(@RequestBody List<String> ids){
        reservoirService.deleteAllById(ids);
        return Result.OK();
    }


    /*查询ok*/
    @GetMapping
    @ApiOperation("查询+分页")
    @ApiImplicitParams({@ApiImplicitParam(value = "pageNum", name = "当前页数", required = true),
            @ApiImplicitParam(value = "pageSize", name = "每页条数", required = false)})
    public Result getAllreservoir(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        PageResult<Reservoir> pageInfo = reservoirService.getAllreservoir(pageNum,pageSize);
        return Result.OK().setData(pageInfo);
    }

    //模糊查询ok
    @ApiOperation("根据库区名称模糊查询")
    @ApiImplicitParam(name = "库区名称",value = "name",required = true)//必填
    @GetMapping("getreservoirByName")
    public Result getreservoirByName(String name){
        List<Reservoir> reservoirList = reservoirService.getreservoirByName(name);
        return Result.OK().setData(reservoirList);
    }


    //回显根据id查询数据ok
    @ApiImplicitParam(name = "库区id",value = "id",required = true)
    @GetMapping("getById")
    public Result getById(Integer id){
        Reservoir reservoir = reservoirService.getById(id);
        return Result.OK().setData(reservoir);
    }


    /*修改*/
    @PutMapping
    @ApiOperation("修改库区接口")
    @ApiImplicitParam(name = "库区对象",value = "reservoir",required = false)
    public Result updatereservoir(@RequestBody Reservoir reservoir){
        reservoirService.updatereservoir(reservoir);
        return Result.OK();
    }
}
